<template>
  <div class="page">
    <TopBar title="修改资料"></TopBar>
    <div class="item">
      <div>头像</div>
      <el-upload :action="host + '/admin/upload/img'" :show-file-list="false" :on-success="upload_act" name="img">
        <div><img class="img" :src="user_info.portrait"></div>
      </el-upload>
    </div>
    <div class="item">
      <div>昵称</div>
      <div class="ipt"><input v-model="user_info.c_name"/></div>
    </div>
    <div class="item">
      <div>手机号</div>
      <div class="ipt"><input v-model="user_info.mobile"/></div>
    </div>

    <div>
      <el-button class="save" @click="save_edit">保存修改</el-button>
    </div>
    <div>
      <el-button class="logout" @click="goPage()">注销账号</el-button>
    </div>
  </div>
</template>

<script>
import TopBar from "@/components/TopBar";

export default {
  name: "EditUserInfo.vue",
  components: {
    TopBar
  },
  data() {
    return {
      host: "http://127.0.0.1:5000/",
      user_info: {
        avatar: "",
        username: "",
        user_id: "",
        mobile: "",
      },
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    getData() {
      this.$axios.get("customer/userinfo/",{
        headers:{
          'Authorization':localStorage.getItem('token') || ''
        }
      }).then(res => {
        if (res.data.code == 200) {
          this.user_info = res.data.data
        }
      })
    },
    save_edit() {
      this.$axios.put("customer/userinfo/", this.user_info,{
        headers:{
          'Authorization':localStorage.getItem('token') || ''
        }
      }).then(res => {
        if (res.data.code == 200) {
          this.$message.success(res.data.msg)
        } else {
          this.$message.error(res.data.msg)
        }
      })
    },
    upload_act(res) {
      if (res.code == 20000) {
        this.user_info.avatar = res.data.filepath
      } else {
        this.$message.error(res.message)
      }
    },
    goPage(){
         localStorage.removeItem("token")
      this.$router.push("login")
    }
  }
}
</script>

<style scoped>

.item {
  display: flex;
  justify-content: space-between;
  margin: 10px 20px;
  align-items: center;
  border-bottom: 1px solid #DDDDDD;
  padding: 15px 2px;
}

.img {
  width: 50px;
  height: 50px;
  border-radius: 25px;
}

.ipt {
  width: 50%;
  text-align: right;
}

.ipt input {
  border: none;
  text-align: right;
}

.logout, .save, .logout:hover, .save:hover {
  width: calc(100% - 40px);
  background-color: rgb(97, 191, 167);
  color: #FFFFFF;
  border: none;
  margin: 50px 20px 0;
}

.logout, .logout:hover {
  margin-top: 20px;
  background-color: #FFFFFF;
  color: #999999;
  border: 1px solid #DDDDDD;
}

</style>